package com.agw.crm.workbench.service.impl;

import com.agw.crm.utils.DateTimeUtil;
import com.agw.crm.utils.SqlSessionUtil;
import com.agw.crm.utils.UUIDUtil;
import com.agw.crm.workbench.dao.CustomerDao;
import com.agw.crm.workbench.dao.TranDao;
import com.agw.crm.workbench.dao.TranHistoryDao;
import com.agw.crm.workbench.domain.Customer;
import com.agw.crm.workbench.domain.Tran;
import com.agw.crm.workbench.domain.TranHistory;
import com.agw.crm.workbench.service.TranService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TranServiceImpl implements TranService {
    private TranDao tranDao = SqlSessionUtil.getSqlSession().getMapper(TranDao.class);
    private TranHistoryDao tranHistoryDao = SqlSessionUtil.getSqlSession().getMapper(TranHistoryDao.class);

    private CustomerDao customerDao = SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);

    @Override
    public boolean save(Tran tran, String customerName) {
        boolean flag = true;

        /*
        一共需要分三步
        1、先查询客户表中受否存在这个客户，不存在就要创建一个
        2、将客户id添加到交易中，保存这条交易
        3、生成交易历史
         */

        //1、查询客户是否存在
        Customer customer = customerDao.getCustomerByName(customerName);
        if (customer == null){
            customer = new Customer();
            customer.setId(UUIDUtil.getUUID());
            customer.setName(customerName);
            customer.setCreateBy(tran.getCreateBy());
            customer.setCreateTime(DateTimeUtil.getSysTime());
            customer.setContactSummary(tran.getContactSummary());
            customer.setNextContactTime(tran.getNextContactTime());
            customer.setOwner(tran.getOwner());

            int count1 = customerDao.save(customer);
            if (count1 != 1){
                flag = false;
            }
        }

        //2、将客户id保存到交易中，保存交易
        tran.setCustomerId(customer.getId());
        int count2 = tranDao.save(tran);
        if (count2 != 1){
            flag = false;
        }

        //3、生成交易历史
        TranHistory tranHistory = new TranHistory();
        tranHistory.setId(UUIDUtil.getUUID());
        tranHistory.setTranId(tran.getId());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setStage(tran.getStage());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setCreateBy(tran.getCreateBy());
        tranHistory.setCreateTime(DateTimeUtil.getSysTime());

        int count3 = tranHistoryDao.save(tranHistory);
        if (count3 != 1){
            flag = false;
        }

        return flag;
    }

    @Override
    public Tran detail(String id) {
        Tran tran = tranDao.detail(id);
        return tran;
    }

    @Override
    public List<TranHistory> getTranHistoryByTranId(String tranId) {
        List<TranHistory> thList = tranHistoryDao.getTranHistoryByTranId(tranId);
        return thList;
    }

    @Override
    public boolean changeStage(Tran tran) {
        boolean flag = true;
        int count1 = tranDao.changeStage(tran);
        if (count1!=1){
            flag = false;
        }

        //添加阶段历史
        TranHistory th = new TranHistory();
        th.setId(UUIDUtil.getUUID());
        th.setStage(tran.getStage());
        th.setExpectedDate(tran.getExpectedDate());
        th.setCreateBy(tran.getEditBy());
        th.setCreateTime(tran.getEditTime());
        th.setTranId(tran.getId());
        th.setMoney(tran.getMoney());

        int count2 = tranHistoryDao.save(th);
        if (count2!=1){
            flag = false;
        }

        return flag;
    }

    @Override
    public Map<String, Object> getCharts() {
        //获取total
        int total = tranDao.getTotal();
        //获取dataList
        List<Map<String,Object>> dataList = tranDao.getDataList();
        //将total和dataList保存到map集合中
        Map<String ,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("dataList",dataList);

        return map;
    }
}
